<?php
namespace App\Http\Controllers\Business;

use App\Http\Controllers\Business\MatlabTool;

class MatlabModule {
    public function suanFa($Q, $D) {
        //$Q="906,14000;835,15000;763,16000;690,17000;615,18000;536,19000;453,20000;363,21000;313,21500;260,22000;248,22100;182,22600;167,22700";
        //Q=[872,10000;775,11000;678,12000;578,13000;474,14000;365,15000;307,15500;245,16000;176,16500;161,16600;145,16700;127,16800;109,16900];
        //$D="2,2,2,2,10,10,10,50,50,11,5,1";//确定各区间提取数量，开头默认取1个数据
        //D=[2,2,2,20,20,38,22,40,5,2,1,1];%确定各区间提取数量，开头默认取1个数据
        // Qh = zeros(length(Q)+2,1);
        // for i=1:length(Q)
        // Qh(i+1,1)=Q(i,1);
        // Qu = zeros(length(Q)+2,1);
        // for i=1:length(Q)
        // Qu(i+1,1)=Q(i,2);
        $QArray = explode(';', $Q);
        $QhArray = [0, 0];
        $QuArray = [0, 0];
        foreach($QArray as $QList) {
            $secondQList = explode(',', $QList);
            array_push($QhArray, trim($secondQList[0]));
            array_push($QuArray, trim($secondQList[1]));
        }
        $DArray = explode(',', $D);
        //* n=length(Q);
        // A=zeros(n+2);
        // A(1,1)=2;A(1,2)=-5;A(1,3)=4;A(1,4)=-1;
        // A(n+2,n-1)=-1;A(n+2,n)=4;A(n+2,n+1)=-5;A(n+2,n+2)=2;
        // for i=2:(n+1)
        // A(i,i-1)=1;
        //    * A(i,i)=4;
        //    * A(i,i+1)=1;
        //    * end
        $n = count($QhArray);
        $A = [ ];
        for($i=0; $i<$n+2; $i++) {
            for($j=0; $j<$n+2; $j++) {
                if ($i == 0 && $j == 0) {
                    $A[$i][$j] = 2;
                } elseif ($i == 0 && $j == 1) {
                    $A[$i][$j] = -5;
                } elseif ($i == 0 && $j == 2) {
                    $A[$i][$j] = 4;
                } elseif ($i == 0 && $j == 3) {
                    $A[$i][$j] = -1;
                } elseif ($i == ($n + 1) && $j == $n - 2) {
                    $A[$i][$j] = -1;
                } elseif ($i == ($n + 1) && $j == ($n - 1)) {
                    $A[$i][$j] = 4;
                } elseif ($i == ($n + 1) && $j == $n) {
                    $A[$i][$j] = -5;
                } elseif ($i == ($n + 1) && $j == ($n + 1)) {
                    $A[$i][$j] = 2;
                } elseif($i == $j-1) {
                    $A[$i][$j] = 1;
                } elseif($i == $j) {
                    $A[$i][$j] = 4;
                } elseif($i == $j+1) {
                    $A[$i][$j] = 1;
                } else {
                    $A[$i][$j] = 0;
                }
            }
        }
        $matlabTool = new MatlabTool();

        if(!$matlabTool->_matrix_well_formed($A) || !$matlabTool->_matrix_well_formed($QhArray) ) {
            //return false;
        }
        $invA = $matlabTool->getNiMatrix($A);
        $APlus6 = $matlabTool->matrix_scalar_operation($invA, 6, '*');
        $Ph = $matlabTool->matrix_operation($APlus6, $QhArray, '*');
        $Pu = $matlabTool->matrix_operation($APlus6, $QuArray, '*');
	    $B = [[1,4,1,0],[-3,0,3,0],[3,-6,3,0],[-1,3,-3,1]];
	    $N = count($Ph[0]) + count($Pu[0]) - 3; //TODO
        $E = Array();
        /*
        * Ph=6*inv(A)*Qh;%Ph控制点列横坐标，Ph=6A^(-1)Qh,Qh为取值点的横坐标
        * Pu=6*inv(A)*Qu;%Ph控制点列纵坐标，Pu=6u^(-1)Qu,Qh为取值点的纵坐标
        * P=[Ph,Pu];%求控制点列值
        * N=length(P)-3;
        * B=[1,4,1,0;-3,0,3,0;3,-6,3,0;-1,3,-3,1];
        * for i=1:N
            * M=Qu(i+2)-Qu(i+1);
                * for c=0:M
                * Ch=[Ph(i);Ph(i+1);Ph(i+2);Ph(i+3)];%取P点4个横坐标值
                * Cu=[Pu(i);Pu(i+1);Pu(i+2);Pu(i+3)];%取P点4个纵坐标值
                * t=c/M;%取等分点
                * T=[1,t,t^2,t^3];%T横向量
                * E(c+1,2*i-1)=1/6*T*B*Ch;%生成数据每行奇数位，即液位值
                * E(c+1,2*i)=1/6*T*B*Cu;%生成数据每行偶数位，即容量值
                * end
                * end
             *
         */
        for($i=0; $i<$N; $i++) {
            $M = $QuArray[$i+2] - $QuArray[$i+1];
            for($c=0; $c< $M; $M++) {
                $Ch = [$Ph[$i],$Ph[$i+1], $Ph[$i+2], $Ph[$i+3]];
                $Cu = [$Pu[$i],$Pu[$i+1], $Pu[$i+2], $Pu[$i+3]];
                $t = $c / $M;
                $T = [1, $t, $t * $t, $t*$t*$t];
                $One6 =  $matlabTool->matrix_scalar_operation($T, 1/6, '*');
                $TBArray =  $matlabTool->matrix_operation($One6, $B, '*');
                $E[$c+1][$i*2-1] = $matlabTool->matrix_operation($TBArray, $Ch, '*');
                $E[$c+1][$i*2]  = $matlabTool->matrix_operation($TBArray, $Cu, '*');
            }
        }
        $F = array();
        /* %整理数据
        * for i=1:N
        * G=Qh(i+1)-Qh(i+2);
            * M=Qu(i+2)-Qu(i+1);
            * z=0;
            * for y=0:G
            * for c=0:M
            * if 1    %加判断
                * if abs(E(c+1,2*i-1)-(Qh(i+1)-y))<=0.18
            * z=z+1;
                    * F(z,2*i-1)=round(E(c+1,2*i-1));
                    * F(z,2*i)=round(E(c+1,2*i));
                * break
                * end
                * end
                * end
                * end
                * end
         *
         */
        for($i=0; $i<$N; $i++) {
            $G = $QhArray[$i+1] - $QhArray[$i+2];
            $M = $QuArray[$i+2] - $QuArray[$i+1];
            $z = 0;
            for($y=0; $y<$G; $y++) {
                for ($c=0; $c<$M; $c++) {
                    if(abs($E[$c+1][$i*2-1]-$QhArray[$i+1]+$y) <= 0.18) {
                        $z +=1;
                        $F[$z][$i*2-1] = round($E[$c+1][$i*2-1]);
                        $F[$z][$i*2] = round($E[$c+1][$i*2]);
                    }
                }
            }
        }
       /* s=0;
        * for i=1:N
        * G=Qh(i+1)-Qh(i+2);
        * z=G/D(i);
        * for y=1:z:G
            * s=s+1;
            * T(s,1)=F(round(y),2*i-1);
            * T(s,2)=F(round(y),2*i);
            * end
        * end
        * T(156,1)=Q(N+1,1);
        * T(156,2)=Q(N+1,2);
        */
        $s = 0;
        $T = array();
        for($i=0; $i<$N; $i++) {
            $G = $QhArray[$i+1] - $QhArray[$i+2];
            $z = $G / $DArray[$i];
            for ($y=0; $y< $G; $y+=$z) {
                $s = $s + 1;
                $T[$s][0] = $F[round($y)][$i*2-2];
                $T[$s][1] = $F[round($y)][$i*2-1];
            }
        }
        /* for i=1:4
        * for j=1:39
        * Z(j,2*i-1)=T(j+39*(i-1),1);
            * Z(j,2*i)=T(j+39*(i-1),2);
            * end
        * end
        */
        $Z = array();
        $T[156][0] = $QhArray[$N];
        $T[156][1] = $QuArray[$N];
        for ($i=0; $i<4; $i++) {
            for($j=0; $j<39; $j++) {
                $Z[$j][$i*2-1] = $T[$j+39*($i-1)][0];
                $Z[$j][$i*2] = $T[$j+39*($i-1)][1];
            }
        }
        $data = [
            'F' => $F,
            'Z' => $Z
        ];
        return $data;
/*

* %制作表格
* x=' ';%空格
* xlswrite('A.xlsx',x,1,'A1:H40');%清空表格
* xlswrite('A.xlsx',x,2,'A1:X1002');%清空表格
* for i=1:4
    * headers{1,2*i-1} =['液位(mm)'];
     * headers{1,2*i} =['容量(L)'];
* end
* xlswrite('A.xlsx',headers,1,'A1');
* xlswrite('A.xlsx',Z,1,'A2');
* for i=1:N
    * headers{1,2*i-1} =['液位(mm)'];
     * headers{1,2*i} =['容量(L)'];
* end
* xlswrite('A.xlsx',headers,2,'A1');
* xlswrite('A.xlsx',F,2,'A2');
* disp('表格生成成功...');
* disp('结束！');
    */
        
}
    public function suanFaData() {
        $data = [
            [920,11000, 828, 12000, 734,13000,637,14000,538,15000,435,16000,379,16500,319,17000,247,17500,229,17600],
            [919,11009,	827, 12009,	733,13009,636,14009,537,15009,434,16009,378,16508,318,17007,246,17506,228,17603],
            [918,11020,	826,12020,	732,13019,	635,	14019,	536,	15018,	433,	16018,	377,	16516,	317,	17014,	245,	17514,	227,	17607],
            [917,11031,	825,12031,	731,13030,	634,	14029,	535,	15029,	432,	16027,	376,	16525,	316,	17022,	244,	17519,	226,	17611],
            [916,11042,	824,12042,	730,13040,	633,	14039,	534,	15039,	431,	16037,	375,	16534,	315,	17029,	243,	17527,	225,	17615],
            [915,11053,	823,12053,	729,13051,	632,	14050,	533,	15048,	430,	16045,	374,	16543,	314,	17037,	242,	17533,	224,	17619],
            [914,11064,	822,12063,	728,13061,	631,	14060,	532,	15058,	429,	16055,	373,	16552,	313,	17044,	241,	17538,	223,	17623],
            [913,11075,	821,12074,	727,13071,	630,	14071,	531,	15069,	428,	16065,	372,	16560,	312,	17052,	240,	17545,	222,	17626],
            [912,11086,	820,12085,	726,13082,	629,	14080,	530,	15078,	427,	16074,	371,	16569,	311,	17059,	239,	17551,	221,	17631],
            [911,11097,	819,12096,	725,	13092,	628,	14090,	529,	15088,	426,	16083,	370,	16577,	310,	17066,	238,	17557,	220,	17635],
            [910,11108,	818,12106,	724,	13103,	627,	14101,	528,	15098,	425,16092,	369,	16586,	309,	17074,	237,	17563,	219,	17639],
            [909,11119,	817,12117,	723,	13114,	626,	14111,	527,	15108,	424,	16102,	368,	16596,	308,	17081,	236,	17569,	218,	17644],
            [908,11130,	816,12128,	722,	13124,	625,	14121,	526,	15117,	423,	16110,	367,	16604,	307,	17088,	235,	17575,	217,	17648],
            [907,11141,	815,12139,	721,	13134,	624,	14131,	525,	15127,	422,	16119,	366,	16613,	306,	17096,	234,	17580,	216,	17652],
            [906,11151,	814,12150,	720,	13145,	623,	14142,	524,	15137,	421,	16129,	365,	16621,	305,	17103,	233,	17585,	215,	17656],
            [905,11162,	813,12160,	719,	13155,	622,	14152,	523,	15148,	420,	16137,	364,	16630,	304,	17110,	232,	17590,	214,	17660],
            [904,11173,	812,12171,	718,	13166,	621,	14162,	522,	15158,	419,	16147,	363,	16639,	303,	17117,	231,	17593,	213,	17665],
            [903,11184,	811,12182,	717,	13176,	620,  14172,	521,	15167,	418,	16156,	362,	16648,	302,	17124,	230,	17597,	212,	17669],
	    [902,11195,	810,12193,	716,	13186,	619,	14182,	520,	15177,	417,	16166,	361,	16656,	301,	17132,	229,	17600],
[901,	11206,	809,	12203,	715,	13196,	618,	14193,	519,	15187,	416,	16175,	360,	16665,	300,	17139,	0,	0],
[900,	11217,	808,	12214,	714,	13207,	617,	14202,	518,	15197,	415,	16184,	359,	16674,	299,	17146,	0,	0],
[899,	11228,	807,	12225,	713,	13217,	616,	14213,	517,	15207,	414,	16192,	358,	16682,	298,	17153,	0,	0],
[898,	11239,	806,	12236,	712,	13227,	615,	14223,	516,	15216,	413,	16201,	357,	16691,	297,	17159,	0,	0],
[897,	11250,	805,	12246,	711,	13238,	614,	14234,	515,	15226,	412,	16211,	356,	16699,	296,	17167,	0,	0],
[896,	11260,	804,	12257,	710,	13248,	613,	14243,	514,	15236,	411,	16219,	355,	16708,	295,	17174,	0,	0],
[895,	11271,	803,	12268,	709,	13259,	612,	14254,	513,	15246,	410,	16228,	354,	16717,	294,	17181,	0,	0],
[894,	11282,	802,	12279,	708,	13270,	611,	14264,	512,	15256,	409,	16237,	353,	16725,	293,	17188,	0,	0],
[893,	11293,	801,	12288,	707,	13280,	610,	14274,	511,	15265,	408,	16246,	352,	16734,	292,	17195,	0,	0],
[892,	11304,	800,	12299,	706,	13290,	609,	14284,	510,	15275,	407,	16255,	351,	16742,	291,	17202,	0,	0],
[891,	11315,	799,	12310,	705,	13300,	608,	14294,	509,	15285,	406,	16264,	350,	16750,	290,	17209,	0,	0],
[890,	11326,	798,	12320,	704,	13311,	607,	14305,	508,	15295,	405,	16272,	349,	16759,	289,	17216,	0,	0],
[889,	11337,	797,	12331,	703,	13321,	606,	14315,	507,	15305,	404,	16282,	348,	16768,	288,	17223,	0,	0],
[888,	11348,	796,	12342,	702,	13331,	605,	14325,	506,	15314,	403,	16290,	347,	16776,	287,	17230,	0,	0],
[887,	11359,	795,	12353,	701,	13342,	604,	14335,	505,	15324,	402,	16299,	346,	16784,	286,	17237,	0,	0],
[886,	11369,	794,	12363,	700,	13352,	603,	14345,	504,	15334,	401,	16308,	345,	16792,	285,	17244,	0,	0],
[885,	11380,	793,	12374,	699,	13362,	602,	14356,	503,	15344,	400,	16317,	344,	16801,	284,	17251,	0,	0],
[884,	11391,	792,	12385,	698,	13372,	601,	14366,	502,	15353,	399,	16326,	343,	16809,	283,	17258,	0,	0],
[883,	11402,	791,	12395,	697,	13383,	600,	14375,	501,	15363,	398,	16334,	342,	16818,	282,	17265,	0,	0],
[882,	11413,	790,	12406,	696,	13393,	599,	14386,	500,	15373,	397,	16343,	341,	16826,	281,	17271,	0,	0],
[881,	11424,	789,	12417,	695,	13403,	598,	14396,	499,	15383,	396,	16351,	340,	16833,	280,	17279,	0,	0],
[880,	11435,	788,	12427,	694,	13414,	597,	14406,	498,	15393,	395,	16361,	339,	16842,	279,	17285,	0,	0],
[879,	11446,	787,	12438,	693,	13424,	596,	14417,	497,	15403,	394,	16369,	338,	16850,	278,	17293,	0,	0],
[878,	11457,	786,	12449,	692,	13435,	595,	14426,	496,	15413,	393,	16378,	337,	16858,	277,	17299,	0,	0],
[877,	11467,	785,	12460,	691,	13445,	594,	14436,	495,	15422,	392,	16386,	336,	16867,	276,	17306,	0,	0],
[876,	11478,	784,	12470,	690,	13456,	593,	14447,	494,	15432,	391,	16395,	335,	16874,	275,	17312,	0,	0],
[875,	11489,	783,	12481,	689,	13466,	592,	14457,	493,	15442,	390,	16403,	334,	16883,	274,	17320,	0,	0],
[874,	11500,	782,	12492,	688,	13476,	591,	14467,	492,	15451,	389,	16412,	333,	16891,	273,	17326,	0,	0],
[873,	11511,	781,	12502,	687,	13486,	590,	14477,	491,	15461,	388,	16421,	332,	16898,	272,	17333,	0,	0],
[872,	11522,	780,	12513,	686,	13497,	589,	14488,	490,	15470,	387,	16430,	331,	16906,	271,	17340,	0,	0],
[871,	11533,	779,	12523,	685,	13507,	588,	14497,	489,	15481,	386,	16438,	330,	16915,	270,	17347,	0,	0],
[870,	11544,	778,	12534,	684,	13517,	587,	14507,	488,	15491,	385,	16447,	329,	16922,	269,	17354,	0,	0],
[869,	11554,	777,	12545,	683,	13527,	586,	14517,	487,	15500,	384,	16456,	328,	16930,	268,	17360,	0,	0],
[868,	11565,	776,	12555,	682,	13538,	585,	14528,	486,	15510,	383,	16464,	327,	16938,	267,	17366,	0,	0],
[867,	11576,	775,	12566,	681,	13548,	584,	14538,	485,	15519,	382,	16473,	326,	16946,	266,	17374,	0,	0],
[866,	11587,	774,	12577,	680,	13558,	583,	14548,	484,	15530,	381,	16482,	325,	16954,	265,	17380,	0,	0],
[865,	11598,	773,	12587,	679,	13568,	582,	14558,	483,	15539,	380,	16490,	324,	16961,	264,	17387,	0,	0],
[864,	11609,	772,	12598,	678,	13579,	581,	14568,	482,	15548,	379,	16499,	323,	16968,	263,	17393,	0,	0],
[863,	11620,	771,	12609,	677,	13589,	580,	14578,	481,	15559,	0,	0,	322,	16976,	262,	17401,	0,	0],
[862,	11630,	770,	12619,	676,	13599,	579,	14588,	480,	15568,	0,	0,	321,	16984,	261,	17407,	0,	0],
[861,	11641,	769,	12630,	675,	13610,	578,	14599,	479,	15577,	0,	0,	320,	16992,	260,	17414,	0,	0],
[860,	11652,	768,	12640,	674,	13620,	577,	14609,	478,	15588,	0,	0,	319,	16999,	259,	17421,	0,	0],
[859,	11663,	767,	12651,	673,	13630,	576,	14619,	477,	15597,	0,	0,	0,	0,	258,	17426,	0,	0],
[858,	11674,	766,	12662,	672,	13640,	575,	14629,	476,	15607,	0,	0,	0,	0,	257,	17433,	0,	0],
[857,	11685,	765,	12672,	671,	13651,	574,	14639,	475,	15616,	0,	0,	0,	0,	256,	17440,	0,	0],
[856,	11696,	764,	12683,	670,	13661,	573,	14649,	474,	15627,	0,	0,	0,	0,	255,	17447,	0,	0],
[855,	11706,	763,	12693,	669,	13671,	572,	14659,	473,	15636,	0,	0,	0,	0,	254,	17454,	0,	0],
[854,	11717,	762,	12704,	668,	13681,	571,	14669,	472,	15646,	0,	0,	0,	0,	253,	17460,	0,	0],
[853,	11728,	761,	12714,	667,	13692,	570,	14679,	471,	15655,	0,	0,	0,	0,	252,	17466,	0,	0],
[852,	11739,	760,	12725,	666,	13702,	569,	14689,	470,	15665,	0,	0,	0,	0,	251,	17473,	0,	0],
[851,	11750,	759,	12736,	665,	13712,	568,	14699,	469,	15675,	0,	0,	0,	0,	250,	17480,	0,	0],
[850,	11761,	758,	12746,	664,	13723,	567,	14709,	468,	15685,	0,	0,	0,	0,	249,	17486,	0,	0],
[849,	11771,	757,	12757,	663,	13733,	566,	14720,	467,	15694,	0,	0,	0,	0,	248,	17493,	0,	0],
[848,	11782,	756,	12767,	662,	13743,	565,	14729,	466,	15703,	0,	0,	0,	0,	247,	17499,	0,	0],
[847,	11793,	755,	12778,	661,	13753,	564,	14739,	465,	15713,	0,	0,	0,	0,	0,	0,	0,	0],
[846,	11804,	754,	12788,	660,	13764,	563,	14749,	464,	15723,	0,	0,	0,	0,	0,	0,	0,	0],
[845,	11815,	753,	12799,	659,	13774,	562,	14759,	463,	15733,	0,	0,	0,	0,	0,	0,	0,	0],
[844,	11826,	752,	12809,	658,	13784,	561,	14769,	462,	15742,	0,	0,	0,	0,	0,	0,	0,	0],
[843,	11836,	751,	12820,	657,	13794,	560,	14780,	461,	15752,	0,	0,	0,	0,	0,	0,	0,	0],
[842,	11847,	750,	12830,	656,	13804,	559,	14789,	460,	15761,	0,	0,	0,	0,	0,	0,	0,	0],
[841,	11858,	749,	12841,	655,	13814,	558,	14799,	459,	15771,	0,	0,	0,	0,	0,	0,	0,	0],
[840,	11869,	748,	12851,	654,	13825,	557,	14809,	458,	15780,	0,	0,	0,	0,	0,	0,	0,	0],
[839,	11880,	747,	12862,	653,	13835,	556,	14820,	457,	15790,	0,	0,	0,	0,	0,	0,	0,	0],
[838,	11890,	746,	12873,	652,	13845,	555,	14829,	456,	15800,	0,	0,	0,	0,	0,	0,	0,	0],
[837,	11901,	745,	12884,	651,	13856,	554,	14839,	455,	15809,	0,	0,	0,	0,	0,	0,	0,	0],
[836,	11912,	744,	12894,	650,	13866,	553,	14850,	454,	15819,	0,	0,	0,	0,	0,	0,	0,	0],
[835,	11923,	743,	12905,	649,	13876,	552,	14859,	453,	15828,	0,	0,	0,	0,	0,	0,	0,	0],
[834,	11934,	742,	12915,	648,	13887,	551,	14869,	452,	15838,	0,	0,	0,	0,	0,	0,	0,	0],
[833,	11945,	741,	12926,	647,	13897,	550,	14879,	451,	15847,	0,	0,	0,	0,	0,	0,	0,	0],
[832,	11955,	740,	12936,	646,	13906,	549,	14889,	450,	15857,	0,	0,	0,	0,	0,	0,	0,	0],
[831,	11966,	739,	12946,	645,	13917,	548,	14900,	449,	15867,	0,	0,	0,	0,	0,	0,	0,	0],
[830,	11977,	738,	12957,	644,	13927,	547,	14909,	448,	15876,	0,	0,	0,	0,	0,	0,	0,	0],
[829,	11988,	737,	12967,	643,	13937,	546,	14920,	447,	15886,	0,	0,	0,	0,	0,	0,	0,	0],
[828,	11999,	736,	12978,	642,	13948,	545,	14929,	446,	15895,	0,	0,	0,	0,	0,	0,	0,	0],
[0,	0,	735,	12988,	641,	13958,	544,	14940,	445,	15905,	0,	0,	0,	0,	0,	0,	0,	0],
[0,	0,	734,	12999,	640,	13968,	543,	14949,	444,	15914,	0,	0,	0,	0,	0,	0,	0,	0],
        ];
        return $data;
    }
}
//$matlabModule = new MatlabModule();
//$matlabModule->suanfa();
?>
